@extends('admin.layouts.content')
@section('content')
    <form class="layui-form layui-form-pane" id="add-form" lay-filter="add-form">
        <input type="hidden" value="{{ @$model->id }}" name="id">
        <input type="hidden" value="" name="ids" id="ids">

        <div class="layui-form-item" style="margin-top: 5px;">
            <label class="layui-form-label">报表名称</label>
            <div class="layui-input-inline">
                <input type="text" name="report_name" required lay-verify="required" placeholder="消息分组"
                       autocomplete="off" class="layui-input" value="{{ @$model->report_name }}">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">首页显示</label>
            <div class="layui-input-block">
                <input type="radio" name="is_show_home" value="1" title="是" {{ (@$model->is_show_home ==1) ? 'checked' : '' }}>
                <input type="radio" name="is_show_home" value="0" title="否" {{ (@$model->is_show_home !=1) ? 'checked' : '' }}>
            </div>
        </div>



        <table class="layui-hide" id="create-table" lay-filter="create-table"></table>

        <script type="text/html" id="data-table-top-bar" lay-filter="data-table-top-bar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="add" type="button">添加权限</button>
            </div>
        </script>

        <script type="text/html" id="data-table-bar">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>

            <a class="layui-btn layui-btn-xs " lay-event="delete">删除</a>

        </script>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formSubmit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>


@endsection
@section('script')
    <script>
        layui.use(['jquery', 'form', 'layer','table'], function () {
            $ = layui.jquery;
            var form = layui.form;
            var layer = layui.layer;
            var table = layui.table;
            var commitStatus = true;

            var tableIns = table.render({
                elem: '#create-table'

                ,url:'{{url('admin/api/report/user/apiList')}}'
                ,page: true
                ,cellMinWidth: 80
                   , page: false,
                    limit: Number.MAX_VALUE
                ,where: { report_id: '{{@$model->id}}' }
                ,cols: [[
                    {field:'id', width:80, title: 'ID', sort: true}
                    ,{title: '团队人员', minWidth: 100,templet:function (d) {
                             if(d.get_rs_user){
                                 return d.get_rs_user.username;
                             }
                             return  '';
                    }}
                    ,{field: 'remark',title: '备注',  minWidth: 100,}
                    ,{title: '操作', toolbar: '#data-table-bar', width: 450, fixed:'right'}
                ]]
                ,toolbar: "#data-table-top-bar"
                ,defaultToolbar: ['filter', 'exports', 'print']
                // ,parseData: function (res) {
                //     return {
                //         "code": 0, //解析接口状态
                //         "msg": "获取成功", //解析提示文本
                //         "count": res.data.total, //解析数据长度
                //         "data": res.data.data //解析数据列表
                //     };
                // }
            });


            table.on('toolbar(create-table)', function (obj) {
                if (obj.event == 'add') {
                    eventMap.add(obj);
                }
            });
            table.on('tool(create-table)', function(obj){
                if (obj.event == 'edit') {
                    eventMap.edit(obj.data);
                }else if(obj.event == 'delete'){
                    eventMap.del(obj);
                }
            });

            var eventMap = {
                add: function (form) {
                    layer.open({
                        type: 2,
                        title: "添加权限人员",
                        content: '{{url('admin/report/auth')}}',
                        area: ['100%', '100%']
                    });
                },
                del:function (obj) {
                    layer.confirm('您确定要删除条吗？', {
                        btn: ['确认', '取消']
                    }, function(index, layero){
                        $.ajax({
                            url : '{{url('admin/api/report/authDelete')}}',
                            type : "POST",
                            data : {
                                id : obj.data.id,
                                status: 2,
                                _token: "{{ csrf_token() }}"
                            },
                            dataType : 'json',
                            success : function(result) {
                                if(result.error == 0) {
                                    layer.msg(result.msg);
                                    tableIns.reload();
                                }
                                else {
                                    layer.msg(result.msg, { 'icon': 2 });
                                }
                            }
                        });
                    }, function(index){
                        layer.closeAll();
                    });
                },
                edit:function (obj) {
                    layer.open({
                        type: 2,
                        title: "修改权限人员",
                        content: '{{url('admin/report/auth')}}?id='+obj.id,
                        area: ['100%', '100%']
                    });
                }
            };


            form.on('submit(formSubmit)', function (data) {
                if (commitStatus) {
                    var index = parent.layer.getFrameIndex(window.name);
                    commitStatus = false;

                    var submitUrl = data.field.id ? '{{ url("admin/api/report/create-api") }}' : '{{ url("admin/api/report/create-api") }}';
                    var postData = data.field;

                    $.ajax({
                        headers: {'X-CSRF-TOKEN': "{{ csrf_token() }}"},
                        url: submitUrl,
                        method: "POST",
                        dataType: "json",
                        data: postData,
                        success: function (obj) {
                            commitStatus = true;
                            if (obj.error === 0) {
                                layer.msg('保存成功', {icon: 6});
                                parent.layui.table.reload('data-table'); //重载表格
                                parent.layer.close(index);
                            } else {
                                layer.msg(obj.msg, {icon: 5});
                            }
                        },
                        error: function (e) {
                            layer.msg('服务器错误', {icon: 5});
                        }
                    });
                }
                return false;
            });


            $('.layui-btns .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
        function processs_callback(s, obj){
            var user_ids='';
            console.log(obj.field);
            if(s==1){
               // var tabledata = table.cache["data"]; //获取现有数据

                user_ids +=obj.field.id+',';
                user_ids=user_ids.substring(0, user_ids.lastIndexOf(','));
                 $("#ids").val(user_ids);
                layer.closeAll();
            }
        }
    </script>
@endsection
